import {
  navigateTo,
  redirectTo,
  switchTab,
} from '@tarojs/taro';

export function NavigateTo(
  path: string,
  extra?: Record<string, string>
): Promise<TaroGeneral.CallbackResult> {
  let ext: string = '';
  if (extra) {
    for (let key in extra) {
      ext += `${key}=${extra[key]}&`;
    }

    return navigateTo({
      url: `/pages/${path}/${path}?${ext}`,
    });
  } else {
    return navigateTo({
      url: `/pages/${path}/${path}`,
    });
  }
}

export function RedirectTo(
  path: string
): Promise<TaroGeneral.CallbackResult> {
  return redirectTo({
    url: `/pages/${path}/${path}`,
  });
}

export function SwitchTab(
  path: string
): Promise<TaroGeneral.CallbackResult> {
  return switchTab({
    url: `/pages/${path}/${path}`,
  });
}
